package com.right.health.app.ui.user;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.LinkMovementMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.magic.basic.utils.ToastUtil;
import com.magic.basic.utils.VerifyUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.base.BasePresenterActivity;
import com.right.health.app.bean.AppUserResponse;
import com.right.health.app.bean.ChooseMemberInfo;
import com.right.health.app.constants.AppConfig;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.db.DaoSharedPreferences;
import com.right.health.app.hyphenate.EMClientHelper;
import com.right.health.app.net.UserApi;
import com.right.health.app.push.TagAliasOperatorHelper;
import com.right.health.app.ui.common.ArticleDetailActivity;
import com.right.health.app.ui.main.MainActivity;

/*
 * Created by hopeliao on 2019-09-24
 *
 */
public class LoginActivity extends BasePresenterActivity implements View.OnClickListener, OnHttpListener {

    private boolean mPasswordVisible = false;

    private EditText mPwdEt, mPhoneEt;

    private TextView mAgreementTv, mPrivacyTv;

    private ImageView mSwitchPwdVisibleImage;

    private UserApi mUserApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.login);
    }

    @Override
    public void findView() {
        findViewById(R.id.tv_to_register).setOnClickListener(this);
        findViewById(R.id.iv_pwd_change_visible).setOnClickListener(this);
        findViewById(R.id.btn_forget_pwd).setOnClickListener(this);
        findViewById(R.id.tv_login).setOnClickListener(this);
//        findViewById(R.id.tv_privacy).setOnClickListener(this);

        mPwdEt = (EditText) findViewById(R.id.et_pwd);
        mSwitchPwdVisibleImage = (ImageView) findViewById(R.id.iv_pwd_change_visible);

        mPhoneEt = (EditText) findViewById(R.id.et_phone);

        mAgreementTv = findViewById(R.id.tv_agreement);
//        mPrivacyTv = findViewById(R.id.tv_privacy);
        SpannableString spannableString = new SpannableString("登录即代表您已阅读并同意《服务协议》和《隐私协议》");
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_09C3BE)), 12, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(
                new ClickableSpan() {
                    @Override
                    public void onClick(View v) {
                        ArticleDetailActivity.makeActivity(LoginActivity.this, "服务协议", AppConfig.HTTP_ADDRESS + "/pandWx/agreement/h5_provisions_declare.html");
                    }
                }, 12, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_09C3BE)), 19, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(
                new ClickableSpan() {
                    @Override
                    public void onClick(View v) {
                        ArticleDetailActivity.makeActivity(LoginActivity.this, "隐私协议", AppConfig.HTTP_ADDRESS + "/pandWx/agreement/h5_user_privacy.html");
                    }
                }, 19, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mAgreementTv.setText(spannableString);
        mAgreementTv.setMovementMethod(new LinkMovementMethod());
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_to_register) {

            startIntent(RegisterActivity.class);
        } else if (v.getId() == R.id.iv_pwd_change_visible) {
            mPasswordVisible = !mPasswordVisible;
            if (mPasswordVisible) {
                mPwdEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                mPwdEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            mPwdEt.setSelection(mPwdEt.getText().length());
            mSwitchPwdVisibleImage.setImageResource(mPasswordVisible ?
                    R.mipmap.ic_pwd_change_visible2 :
                    R.mipmap.ic_pwd_change_visible1);
        } else if (v.getId() == R.id.btn_forget_pwd) {
            startIntent(ForgetPwdStep1Activity.class);
        } else if ((v.getId() == R.id.tv_login)) {
            String phone = mPhoneEt.getText().toString().trim();
            if (TextUtils.isEmpty(phone)) {

            } else if (!VerifyUtil.isMobile(phone)) {
                ToastUtil.show(App.getInstance(), R.string.please_enter_correct_phone);
                return;
            }
            String pwd = mPwdEt.getText().toString().trim();
            if (TextUtils.isEmpty(pwd)) {
                ToastUtil.show(App.getInstance(), R.string.please_enter_pwd);
                return;
            }
            mUserApi.login(phone, pwd);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(App.getInstance(), response.error);
        } else {
            if (response.targetData instanceof AppUserResponse) {
                AppUserResponse userResponse = response.cast(AppUserResponse.class);
                App.getInstance().refreshUser(userResponse.user);
                DaoSharedPreferences.getInstance().setManBingInfo(userResponse.user.manbingList);
                App.getInstance().refreshChooseMember(new ChooseMemberInfo(userResponse.user.memberid, userResponse.user.sex));

                sendBroadcast(BroadCastCode.LOGIN_CHANGE);
                ToastUtil.show(App.getInstance(), "登录成功");
                TagAliasOperatorHelper.sequence++;
                TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
                tagAliasBean.action = TagAliasOperatorHelper.ACTION_SET;
                tagAliasBean.isAliasAction = true;
                tagAliasBean.alias = userResponse.user.personid;
                TagAliasOperatorHelper.getInstance().handleAction(getApplicationContext(), TagAliasOperatorHelper.sequence, tagAliasBean);
                EMClientHelper.getInstance().login(AppConfig.EM_DEFAULT_USER + userResponse.user.personid, AppConfig.EM_DEFAULT_PASSWORD);

                startIntent(MainActivity.class);
                finish();
            }

        }
    }
}
